import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ZhongJinTao
 * Date: 2024-03-05
 * Time: 14:56
 */
public class IODemo11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件");
        String srcFileName = scanner.next();
        System.out.println("请输入目标文件");
        String desFileName = scanner.next();

        File srcFile = new File(srcFileName);
        //判断源文件的合法性
        if(!srcFile.isFile()) {
            System.out.println("文件不合法");
            return;
        }
        File desFile = new File(desFileName);
        //判断目标文件的合法性，不要求文件存在，但要求文件路径存在
        if(!desFile.getParentFile().isDirectory()) {
            System.out.println("目标路径不合法");
            return;
        }
        //读取要复制文件的每个字节,把读到的每个字节都放到新的文件中
        try(InputStream srcInputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(desFile)) {
            while (true) {
                byte[] buffer = new byte[1024];
                int n = srcInputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                outputStream.write(buffer,0 ,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
